#include "polygonitem.h"
#include "vertexitem.h"
#include "labelscene.h"
#include <QGraphicsPolygonItem>
#include <QGraphicsSceneContextMenuEvent>
#include <QMenu>
#include <QTimer>
PolygonItem::PolygonItem(LabelScene *scene, QGraphicsItem *parent)
    : QGraphicsPolygonItem(parent)
{
    setFlag(ItemIsMovable, true);
    setFlag(ItemIsSelectable, true);
    setFlag(ItemSendsGeometryChanges, true);
    myScene = scene;
}

QVariant PolygonItem::itemChange(GraphicsItemChange change, const QVariant &value)
{
    if (change == ItemPositionChange && myScene) {
        // qDebug() << "item change" << "scene is" << myScene;
        QPointF newPos = value.toPointF();
        qDebug() << "newPos" << newPos;
        QRectF sceneBounds = myScene->sceneRect();

        // 获取当前多边形
        QPolygonF poly = this->polygon();
        qDebug() << "original polygon is" << poly;
        qDebug() << "this pos is" << pos();
        // 将 polygon 的所有点平移到新位置
        QPolygonF movedPolygon;
        for (const QPointF &point : poly) {
            movedPolygon << point + newPos;
        }
        qDebug() << movedPolygon;
        // 判断是否所有点都在 scene 内
        bool allInside = std::all_of(movedPolygon.begin(), movedPolygon.end(),
                                     [&sceneBounds](const QPointF &p) {
                                         return sceneBounds.contains(p);
                                     });

        if (!allInside) {
            qDebug() << "Move rejected: polygon would be out of bounds.";

            QTimer::singleShot(0, [this]() {
                for (VertexItem* vertex : myVertexes) {
                    vertex->setPos(myPolygon[vertex->index()] + this->pos());
                }
            });

            return pos(); // 返回当前位置，阻止移动
        }

        return newPos; // 正常移动
    }

    return QGraphicsPolygonItem::itemChange(change, value);
}

void PolygonItem::addVertex(VertexItem* vertex, bool beforeIsMoving, bool AfterIsMoving)
{

    if(AfterIsMoving == false)
    {
        if(myPolygon.isEmpty())
        {
            qDebug() << "empty polygon add 实心点";
            myPolygon << vertex->scenePos();


        }
        else
        {
            qDebug() << "add 实心点";
            myPolygon.removeLast();
            myPolygon << vertex->scenePos();
        }
        // qDebug() << "append a vertex";

        myVertexes.append(vertex);
        vertex->setPolygonItem(this);

        // qDebug() << "related polygon is" << this;

    }
    else
    {
        if(beforeIsMoving == false)
        {
            qDebug() << "add hollow point 1";
            myPolygon << vertex->scenePos();

        }
        else
        {
            qDebug() << "add hollow point 2";
            myPolygon.removeLast();
            myPolygon << vertex->scenePos();
        }
    }
    setPolygon(myPolygon);

}

void PolygonItem::addRectangle(VertexItem* vertex, QPointF pos1, QPointF pos2)
{
    qDebug() << "addRectangle";
    myPolygon.removeLast();
    myPolygon.removeLast();
    myPolygon.removeLast();
    myPolygon << pos1;
    myPolygon << vertex->scenePos();
    myPolygon << pos2;
    setPolygon(myPolygon);
}
void PolygonItem::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
{
    if(scene()->mode() == LabelScene::EditPolygon)
    {
        QMenu menu;
        QAction *deleteAction = menu.addAction("删除");
        QAction *modifyAction = menu.addAction("修改文本");
        QAction *selectedAction = menu.exec(event->screenPos());
        if (selectedAction == deleteAction) {
            emit requestDelete(this);
        } else if (selectedAction == modifyAction) {
            emit requestModify(this);
        }
        qDebug() << "context menu event";
    }

}
